//
//  SearchViewController.m
//  DMGME
//
//  Created by MC on 15/9/28.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "SearchViewController.h"
#import "DMCustomViewController.h"
#import "RBCustomDatePickerView.h"
#import "OrderABModel.h"
#import "OrderGoodsModel.h"
#import "OrderDetaiABlViewCell.h"
#import "headerView.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{
    CGRect rectz;
    NSString *_mIds;
    UIView *_alertView;
    UIView *_hug;
    RBCustomDatePickerView *_pickerView;
    NSString *_btnStr1;
    NSString *_btnStr2;
    
    long int _section;
    NSString *_mId;
    headerView *_hview;
    
    NSArray *mHourArray;
    NSMutableArray *mDateArray;
    //记录今天的年月日时
    int mYear;
    int mMonth;
    int mDate;
    int mHour;
    
    int mSelectedDateIndex; //存储今天年月的数组下标
    int mSelectedhourIndex; //存储今天小时的下标
    UIPickerView *pickerViews;

}
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,strong)UILabel *tishiLabel;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"搜索";
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _datas = [NSMutableArray array];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, KWidth, KHeight-10)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewScrollPositionNone;
    [self.view addSubview:_tableView];
    _tishiLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, _tableView.frame.origin.x+20, KWidth-200, 20)];
    _tishiLabel.textAlignment = NSTextAlignmentCenter;
    _tishiLabel.font = [UIFont systemFontOfSize:12];
    _tishiLabel.textColor = [UIColor grayColor];
    _tishiLabel.text = @"目前暂无数据";
    _tishiLabel.hidden = YES;
    [_tableView addSubview:_tishiLabel];
    [_tableView registerClass:[OrderDetaiABlViewCell class] forCellReuseIdentifier:@"cell"];
    [self downLoadData];
}
-(void)downLoadData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if ([_is  isEqual: @"toBeConfirmed"]) {
        [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$toBeConfirmed"] forKey:@"token"];
    }else
        if ([_is isEqual: @"toBePickup"]) {
            [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$toBePickup"] forKey:@"token"];
        }

    [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setValue:_is forKey:@"action"];
    [dict setObject:_wd forKey:@"wd"];
    [self StartLoading];
    
    [AFDownManager POST:OrderDetailOne parameters:dict success:^(id responseObject) {
        [self StopLoading];
//        NSLog(@"dict ===== %@",dict);
//        NSLog(@"搜索 ===== %@",responseObject);
        if(_datas.count != 0)
        {
            [_datas removeAllObjects];
        }
        
        //        NSLog(@"responseObject == %@",responseObject);
        if ([responseObject[@"data"][@"listData"] count] != 0) {
            NSArray *arr = responseObject[@"data"][@"listData"];
            for (NSDictionary *dic in arr) {
                OrderABModel *model = [OrderABModel modelWithDic:dic];
                NSMutableArray * array = [NSMutableArray array];
                for (NSDictionary * childDic in dic[@"goods"]) {
                    OrderGoodsModel * goodsModel = [[OrderGoodsModel alloc]init];
                    [goodsModel setValuesForKeysWithDictionary:childDic];
                    [array addObject:goodsModel];
                }
                [model.goodsArray addObjectsFromArray:array];
                [model setValuesForKeysWithDictionary:dic];
                
                [_datas addObject:model];
            }
        }
        else
        {
            _tishiLabel.hidden = NO;
        }
        [_tableView reloadData];
        [_tableView reloadData];
        //取消下拉和上拉的状态
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
    } failure:^(NSError *error) {
        [self StopLoading];
        [self showMsg:@"网络连接故障"];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    OrderABModel * model = [_datas  objectAtIndex:section];
    return model.goodsArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 130;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"cell";
    
    OrderDetaiABlViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    OrderABModel * abModel = [_datas objectAtIndex:indexPath.section ];
    OrderGoodsModel *mode = [[abModel goodsArray] objectAtIndex:indexPath.row];
    
    cell.subModel = mode;
    _mId = [abModel id];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1, KWidth, 0.3)];
    line.backgroundColor = [UIColor lightGrayColor];
    line.alpha = 0.35;
    [cell.contentView addSubview:line];
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.datas.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    _hview = [[headerView alloc]initWithFrame:CGRectMake(0, 0, KWidth, 44)];
    _hview.tag = section + 200;
    _hview.titleLabel.textColor = ComColor;
    _hview.titleLabel.font = [UIFont systemFontOfSize:13];
    _hview.backgroundColor =  self.view.backgroundColor;

    if (self.YesOrNo == 1)
    {
        _hview.titleLabel.text = @"等待取货";
        _section = section;
    }
    else
    {
        _hview.titleLabel.text = @"等待确认";
        
    }
    return _hview;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    OrderABModel * model = [self.datas objectAtIndex:section];
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 90)];
    footView.backgroundColor = [UIColor whiteColor];
    
    UILabel *headerLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, KWidth, 0.3)];
    
    headerLine.backgroundColor = [UIColor lightGrayColor];
    
    [footView addSubview:headerLine];
    
    UILabel *total = [[UILabel alloc] initWithFrame:CGRectMake(KWidth-70, 0, 70, 21)];
    
    total.textColor = [UIColor grayColor];
    total.font = [UIFont systemFontOfSize:14];
//    CGFloat fl = model.all_total.floatValue +model.coupon.floatValue;
    total.text = [NSString stringWithFormat:@"￥%@", model.all_total];
    
    [footView addSubview:total];
  
    NSString *string1 = [[self timeStamaps:model.get_starttime] substringToIndex:16];
    NSString *string2 = [[self timeStamaps:model.get_finishtime] substringWithRange:NSMakeRange(11, 5)];
    NSArray *textLabeArr = @[[NSString stringWithFormat:@"订单生成时间:%@",[self timeStamaps:model.order_time]],[NSString stringWithFormat:@"取货时间:%@",[NSString stringWithFormat:@"%@~%@",string1,string2]],[NSString stringWithFormat:@"订单号:%@",model.order_sn]];
    
    for (int i=0; i<3; i++) {
        UILabel *labe = [[UILabel alloc] initWithFrame:CGRectMake(12, 21*i+27, 230, 21)];
        labe.text = textLabeArr[i];
        labe.font = [UIFont systemFontOfSize:14];
        labe.textColor = [UIColor lightGrayColor];
        [footView addSubview:labe];
        
    }
    if (self.YesOrNo == 1 ||self.YesOrNo == 0) {
        NSArray *buttArr = [NSArray array];
        if (self.YesOrNo == 0) {
            buttArr = @[ @"delayButton.png",@"contactButton.png",@"confirmGoodsButton Copy"];
        }else
        {
            
            buttArr = @[ @"delayButton.png",@"contactButton.png",@"sendGoodsButton@2x"];
        }
        
        for (int i= 0; i<buttArr.count; i++) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            
            button.frame = CGRectMake(KWidth/3+10 + (KWidth/6+15) * i,footView.frame.size.height+5, KWidth/6+10, 31);
            [button setBackgroundImage:[UIImage imageNamed:buttArr[i]] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonWithClick:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 500+section;
            [footView addSubview:button];
        }
    }
       return footView;
}
#pragma mark cell上的Button
-(void)buttonWithClick:(UIButton *)sender
{
    //    NSLog(@"%@_____",[NSValue valueWithCGRect:sender.frame]);
    //    =    i;
    //    NSLog(@"%d______",(int)((sender.frame.origin.x - KWidth/3+10)/ (KWidth/6+15)));
    int i = (int)((sender.frame.origin.x - KWidth/3+10)/ (KWidth/6+15));
    
    OrderABModel * abmodel =[_datas objectAtIndex:(sender.tag-500)];
    
    //    NSLog(@"%@++++",abmodel.id);
    

    if (i == 0) {
        [self createTimeData];
        //获取当前的主window
        UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
        //创建背景button 点击该button 删除主window 上的所有控件
        UIButton *bgViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
        bgViewButton.frame =  keyWin.bounds;
        bgViewButton.backgroundColor = [UIColor colorWithWhite:0.3 alpha:1];
        [bgViewButton addTarget:self action:@selector(cancelbgViewClick) forControlEvents:UIControlEventTouchUpInside];
        bgViewButton.tag = 1000;
        bgViewButton.alpha = 0.4;
        [keyWin addSubview:bgViewButton];
        
        UIView *dateBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 64+(KHeight-64-260)/2, KWidth, 260)];
        dateBgView.backgroundColor = [UIColor whiteColor];
        dateBgView.tag = 1001;
        [keyWin addSubview:dateBgView];
        
        //创建取消和完成按钮
        NSArray *tittleArray = @[@"取消",@"完成"];
        
        for (int i = 0; i < 2; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i*(KWidth-54), 0, 54, 44);
            [button setTitle: tittleArray[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithRed:0.9 green:0.32 blue:0.32 alpha:1] forState:UIControlStateNormal];
            
            if (i == 0) {
                [button addTarget:self action:@selector(ensureOrCancelClick1:) forControlEvents:UIControlEventTouchUpInside];
            }
            else
            {
                [button addTarget:self action:@selector(ensureOrCancelClick2:) forControlEvents:UIControlEventTouchUpInside];
                button.tag = abmodel.id.intValue;
            }

            [dateBgView addSubview:button];
        }
        
        UIImageView *lineImage = [[UIImageView alloc]initWithFrame: CGRectMake(0,43,KWidth, 0.5)];
        lineImage.alpha = 0.5;
        lineImage.image = [UIImage imageNamed:@"80"];
        [dateBgView addSubview:lineImage];
        
        //创建日期选择器
        pickerViews = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, KWidth, 216)];
        pickerViews.backgroundColor = [UIColor whiteColor];
        pickerViews.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;
        pickerViews.delegate = self;
        pickerViews.dataSource = self;
        pickerViews.tag = 1002;
        
        [pickerViews selectRow:mSelectedDateIndex inComponent:0 animated:YES];
        [pickerViews selectRow:mSelectedhourIndex inComponent:1 animated:YES];
        
        [dateBgView addSubview:pickerViews];
        
        
    }
    if (i == 1) {
//        NSLog(@"501");
      
        NSString *targetId = [abmodel umobile];
        NSString *userName = [abmodel unickname];
        NSString *imgStr = [abmodel uavatar];
        //        NSLog(@"===%@ === %@ ===%@",targetId,userName,imgStr);
        DMCustomViewController *custom = [[DMCustomViewController alloc] init];
        custom.targetId = targetId;
        custom.userName = userName;
        custom.titleStr = userName;
        NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
        [de setObject:targetId forKey:@"targetId"];
        [de setObject:userName forKey:@"useName"];
        [de setObject:imgStr forKey:@"imgStr"];
        [de synchronize];
        
        [self.navigationController pushViewController:custom animated:YES];
        
    }
    if (i == 2) {
//        NSLog(@"502");
        
      
        if ([[abmodel status] isEqual: @"0"]) {
            [self showMsg:@"商品上架后才能确认留货"];
            return;
        }
        
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        if (abmodel.status.integerValue == 1) {
            [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$setToBePickup"] forKey:@"token"];
            [dict setValue:@"setToBePickup" forKey:@"action"];
            
        }else
        {
            [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$setDelivered"] forKey:@"token"];
            [dict setValue:@"setDelivered" forKey:@"action"];
            
        }
        
        [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
        [dict setValue:abmodel.id forKey:@"id"];
        [self StartLoading];
        [AFDownManager POST:MakeSureButton parameters:dict success:^(id responseObject) {
            [self StopLoading];
//            NSLog(@"data3= %@",responseObject);
            if ([responseObject[@"code"] intValue] == 0) {
                
                //            [self showMsg:@"确认成功"];
                
                if (abmodel.status.integerValue == 1) {
                    [self downLoadData];
                    _is = @"toBeConfirmed";
                }else
                {
                    [self downLoadData];
                    _is = @"toBePickup";
                }
                
                
            }
            else
            {
                [self showMsg:responseObject[@"desc"]];
            }
        } failure:^(NSError *error) {
//            NSLog(@"error == %@",error);
            [self showMsg:@"网络连接故障"];
        }];
        
    }
}

#pragma mark 确认和取消按钮
- (void)ensureOrCancelClick1:(UIButton*)button {
    
    [self cancelbgViewClick];
}
-(void)ensureOrCancelClick2:(UIButton *)button
{
//    UILabel *dateLabel =  (UILabel*)[mLoadView viewWithTag:2000];
//    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
//    UIPickerView *pickView = (UIPickerView*)[[keyWin viewWithTag:1001] viewWithTag:1002];
    int selectDateIndex = (int)[pickerViews selectedRowInComponent:0];
    int selectHourIndex = (int)[pickerViews selectedRowInComponent:1];
    
    NSArray *dateArray = [mDateArray[selectDateIndex] componentsSeparatedByString:@"月"] ;
    NSString *selectStr = nil;
    if ([[dateArray firstObject] integerValue] < mMonth) {
        selectStr = [NSString stringWithFormat:@"%d年%@ %@",mYear+1,mDateArray[selectDateIndex],mHourArray[selectHourIndex]];
//        NSLog(@"============== %@",selectStr);
        
        //        [self updateTimeRequest:button timeStr:selectStr];
        [self updateTimeRequest:button timeStr:selectStr intger:button.tag];
        
    }else{
        selectStr = [NSString stringWithFormat:@"%d年%@ %@",mYear,mDateArray[selectDateIndex],mHourArray[selectHourIndex]];
    }
    
//    dateLabel.text = selectStr;
//    NSLog(@"======== %@",selectStr);
    [self cancelbgViewClick];
    [self updateTimeRequest:button timeStr:selectStr intger:button.tag];
    
}

-(void)updateTimeRequest:(UIButton *)sender timeStr:(NSString *)timeStr intger:(NSInteger )intger{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$"] forKey:@"token"];
    [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)intger] forKey:@"id"];
    
//    NSLog(@"sender = %@",timeStr);
    NSString *string1 = [timeStr substringWithRange:NSMakeRange(0, 4)];
    NSString *string2 = [timeStr substringWithRange:NSMakeRange(5, 2)];
    NSString *string3 = [timeStr substringWithRange:NSMakeRange(8, 2)];
    NSString *string4 = [timeStr substringWithRange:NSMakeRange(12, 2)];
    NSString *string5 = [timeStr substringWithRange:NSMakeRange(17, 2)];
    //   NSLog(@"%@",[self getCurrentDateString:[NSString stringWithFormat:@"%@-%@-%@ %@:00:00",string1,string2,string3,string5]]);
    [dict setObject:[self getCurrentDateString:[NSString stringWithFormat:@"%@-%@-%@ %@:00:00",string1,string2,string3,string4]] forKey:@"get_starttime"];
    [dict setObject:[self getCurrentDateString:[NSString stringWithFormat:@"%@-%@-%@ %@:00:00",string1,string2,string3,string5]] forKey:@"get_finishtime"];
    [self StartLoading];
//    NSLog(@"url = %@,==%@",yanchangButton,dict);
    [AFDownManager POST:yanchangButton parameters:dict success:^(id responseObject) {
        [self StopLoading];
//        NSLog(@"延长收货时间==== %@",responseObject);
        if ([responseObject[@"desc"]  isEqual: @"SUCCESS"]) {
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"成功", nil];
            al.delegate = self;
            [al show];
        }else
        {
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:responseObject[@"desc"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [al show];
            
        }
        
    } failure:^(NSError *error) {
        [self StopLoading];
//        NSLog(@"error == %@",error);
        [self showMsg:@"网络连接故障"];
    }];
    
}
//获取时间戳
- (NSString *)getCurrentDateString:(NSString *)timeStr
{
    NSDate *date = [self dateFromString:timeStr];
    
    long long time = date.timeIntervalSince1970;
    
    NSString *str1 = [NSString stringWithFormat:@"%lld", time];
    
    return str1;
    
}
//字符串转NSDate
- (NSDate *)dateFromString:(NSString *)dateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    return [dateFormatter dateFromString:dateString];
    
}

#pragma mark 关闭日历或时分界面
- (void)cancelbgViewClick{
    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
    
    UIView *bgView = [keyWin viewWithTag:1000];
    UIView *dateHourView = [keyWin viewWithTag:1001];
    [bgView removeFromSuperview];
    [dateHourView removeFromSuperview];
    
    
}
#pragma mark 创建时间
- (void)createTimeData{
    [mDateArray removeAllObjects];
    //记录此时此刻的年月日时分
    //记录今天的年月日
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-HH"];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    //记录预售期的时间
    OrderABModel * model = [_datas  objectAtIndex:_section];
//    NSLog(@"=== %@",model.get_finishtime);
    NSDate *sellTimeDate = [NSDate dateWithTimeIntervalSince1970:[model.get_finishtime doubleValue]];
    NSString *sellDateStr = [dateFormatter stringFromDate:sellTimeDate];
    //判断预售时间是否小于现在
    NSArray *timeArray = nil;
    if ([sellDateStr compare:dateStr options:NSNumericSearch] == NSOrderedAscending) {
        timeArray = [dateStr componentsSeparatedByString:@"-"];
        
    }else{
        timeArray = [sellDateStr componentsSeparatedByString:@"-"];
    }
    mYear = [[timeArray firstObject] intValue];
    mMonth = [[timeArray objectAtIndex:1] intValue];
    mDate = [[timeArray objectAtIndex:2] intValue];
    mHour = [[timeArray lastObject] intValue];
    
    mHourArray = @[@"00 ~ 02点",@"02 ~ 04点",@"04 ~ 06点",@"06 ~ 08点",@"08 ~ 10点",@"10 ~ 12点",@"12 ~ 14点",@"14 ~ 16点",@"16 ~ 18点",@"18 ~ 20点",@"20 ~ 22点",@"22 ~ 00点"];
    mDateArray = [NSMutableArray arrayWithCapacity:10];
    
    for (int i = mMonth; i < 10+mMonth; i++){
        int month = i;
        if (month>12) {
            month-=12;
            
        }
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            for (int j = 1; j < 32; j++) {
                if (j<10) {
                    [mDateArray addObject:[NSString stringWithFormat:@"%02d月%02d日",month,j]];
                }
                else
                {
                [mDateArray addObject:[NSString stringWithFormat:@"%02d月%d日",month,j]];
            }
            }
        }else{
            if (i != 2) {
                for (int j = 1; j < 31; j++) {
                    if (j<10) {
                     [mDateArray addObject:[NSString stringWithFormat:@"%02d月0%d日",month,j]];
                    }
                    else
                    {
                    [mDateArray addObject:[NSString stringWithFormat:@"%02d月%d日",month,j]];
                }
                }
            }else{
                if ((mYear-2004)%4 == 0) {
                    for (int j = 1; j < 29; j++) {
                        [mDateArray addObject:[NSString stringWithFormat:@"%02d月0%d日",month,j]];
                    }
                }else{
                    for (int j = 1; j < 28; j++) {
                        [mDateArray addObject:[NSString stringWithFormat:@"%02d月%d日",month,j]];
                    }
                }
            }
            
        }
        
    }
    
    NSString *todayDateStr = [NSString stringWithFormat:@"%d月%d日",mMonth,mDate];
    for (int i = 0; i < mDateArray.count; i++) {
        if ([mDateArray[i] compare:todayDateStr options:NSNumericSearch] == NSOrderedSame) {
            mSelectedDateIndex = i;
        }
        
    }
    for (int i = 0; i < 24; i++) {
        if (mHour == i ) {
            mSelectedhourIndex = i/2;
            
            
        }
    }
    
    
}
# pragma mark 时间选择器的协议方法

//时间选择器的栏数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
//为时间选择器添加数据源
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return mDateArray.count;
    }else{
        return mHourArray.count;
    }
    
}
//将数据源显示在view上
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return mDateArray[row] ;
    }else{
        return mHourArray[row];
    }
    
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        // NSInteger selectedHourIndex = [pickerView  selectedRowInComponent:1];
        //  NSLog(@"%@",mDateArray[row]);
        if (row<=mSelectedDateIndex) {
            [pickerView selectRow:mSelectedDateIndex inComponent:0 animated:YES];
            [pickerView selectRow:mSelectedhourIndex inComponent:1 animated:YES];
            
        }
    }else{
        NSInteger selectedDateIndex = [pickerView  selectedRowInComponent:0];
        if ((row<mSelectedhourIndex)&&(selectedDateIndex<=mSelectedDateIndex)) {
            [pickerView selectRow:mSelectedhourIndex inComponent:1 animated:YES];
        }
    }
    
}

- (UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:20]];
    }
    // Fill the label text here
    pickerLabel.textColor = [UIColor blackColor];
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    
    if (component == 0) {
//        NSLog(@"%@",mDateArray[row]);
//        NSLog(@"=%ld",(unsigned long)mHourArray.count);
        if (row<mSelectedDateIndex) {
            pickerLabel.textColor = [UIColor lightGrayColor];
        }
    }else{
        NSInteger selectedDateIndex = [pickerView  selectedRowInComponent:0];
        
        if ((row<mSelectedhourIndex)&&(selectedDateIndex<=mSelectedDateIndex)) {
            pickerLabel.textColor = [UIColor lightGrayColor];
        }
    }
    return pickerLabel;
}



-(NSString *)timeStamaps:(NSString *)str
{
    //时间戳转化为时间字符串
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:[str longLongValue]];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *string = [formatter stringFromDate:date];
    //    NSLog(@"date1:%@",str);
    return string;
}
#pragma mark AlterViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (alertView.tag == 900) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://0519-8813239"]];
//        NSLog(@"0519-8813239");
    }
    else
    {
        if (buttonIndex == 1) {
            
            
            OrderABModel *mode = [_datas objectAtIndex:_section];
            if (mode.status.intValue == 1) {
                [self downLoadData];
            }
            if (mode.status.intValue == 2) {
                [self downLoadData];
            }
            
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
